<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use app\admin\model\CaigouDetail as CaigouDetailModel;
use think\Config;
use addons\third\model\Third;

/**
 * 采购
 */
class Caigou extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $this->user_id = $this->auth->id;
        // $this->user_id = 2;
    }  

    /**
     * 采购需求单 非采购员进来会提示,商品为多规格情况下，goods_attr字段会有值
     * @ApirMethod   (POST)
     * @ApiRoute    (/api/Caigou/caigou_list)
     * @ApiReturn   ({
{
  "code": 1,
  "msg": "采购需求单",
  "time": "1600828612",
  "data": {
    "count": 4,
    "list": [
      {
        "id": 9,
        "num": 2,
        "goods_id": 55,
        "goods_spec_id": 100,
        "goods_name": "DIOR迪奥烈艳蓝金唇膏",
        "updatetime": 1600762039,
        "goods_attr": "666 Matte kisss"
      },
      {
        "id": 10,
        "num": 2,
        "goods_id": 56,
        "goods_spec_id": 102,
        "goods_name": "全新迪奥锁妆气垫*",
        "updatetime": 1600762039,
        "goods_attr": ""
      }
    ],
    "yicaigou": 1
  }
}
     * })
     */
    public function caigou_list()
    { 
        if($this->auth->status=='normal')$this->error('您不是采购员');
        $return['count'] = Db::name('caigou')->sum('num');
        $return['list'] = Db::name('caigou')->order('updatetime desc')->select();

        $return['yicaigou'] = Db::name('user')->where('id',$this->user_id)->value('yicaigou');

        $this->success('采购需求单',$return);
    }

    /**
     * 单商品价格录入
     * @ApirMethod   (POST)
     * @ApiRoute    (/api/Caigou/luru)
     * @ApiParams   (name="caigou_id",type=int,required=true,description="采购id")
     * @ApiParams   (name="num",type=int,required=true,description="采购数量")
     * @ApiParams   (name="goods_no",type=string,required=true,description="供应商编号")
     * @ApiParams   (name="unit_price",type=decimal,required=true,description="采购拿货价")
     */

    public function luru(){
      $insert_params = input();
      $caigou_detail = Db::name('caigou')->where('id',$insert_params['caigou_id'])->field('goods_id,goods_name,goods_spec_id,goods_attr')->find();
      $insert_params['user_id'] = $this->user_id;
      $insert_params = array_merge($insert_params,$caigou_detail);

      $insert_params['total_price'] = $insert_params['unit_price']*$insert_params['num'];

      $CaigouDetailModel = new CaigouDetailModel();
      unset($insert_params['caigou_id']);

      $CaigouDetailModel->save($insert_params);

      Db::name('caigou')->where(['goods_id'=>$insert_params['goods_id'],'goods_spec_id'=>$insert_params['goods_spec_id']])->setDec('num',$insert_params['num']);

      $this->success('录入成功');
    }


    /**
     * 已采购数据(按照月份分组,每月的每天只显示其中一条数据)
     * @ApirMethod   (POST)
     * @ApiRoute    (/api/Caigou/has_caigou_list)
     * @ApiReturn   ({
{
  "code": 1,
  "msg": "已采购数据(按照月份分组)",
  "time": "1600828597",
  "data": {
    "2020.09": [
      {
        "id": 3,
        "user_id": 4,
        "goods_id": 55,
        "goods_name": "DIOR迪奥烈艳蓝金唇膏",
        "goods_spec_id": 100,
        "goods_no": "46161616161",
        "unit_price": "1.00",
        "num": 1,
        "total_price": "1.00",
        "createtime": 1600828170,
        "goods_attr": "666 Matte kisss",
        "create_time_text": "2020.09.23"
      }
    ],
    "2020.08": [
      {
        "id": 2,
        "user_id": 4,
        "goods_id": 55,
        "goods_name": "DIOR迪奥烈艳蓝金唇膏",
        "goods_spec_id": 100,
        "goods_no": "46161616161",
        "unit_price": "1.00",
        "num": 1,
        "total_price": "1.00",
        "createtime": 1598149769,
        "goods_attr": "666 Matte kisss",
        "create_time_text": "2020.08.23"
      }
    ]
  }
}

     * })
     */
    public function has_caigou_list(){
       $return = CaigouDetailModel::all(function($query){
          $query->where(['user_id'=>$this->user_id])->order('createtime', 'desc');
        });
       $return = $this->groupVisit($return);
       $this->success('已采购数据(按照月份分组)',$return);
    }
    protected function groupVisit($visit)
    {
        $curyear = date('Y.m');
        $visit_list = [];
        foreach ($visit as $v) {
            if ($curyear == date('Y.m', $v['createtime'])) {
                $date = date('Y.m', $v['createtime']);
            } else {
                $date = date('Y.m', $v['createtime']);
            }
            if(array_key_exists($date, $visit_list)){
              if($visit_list[$date][0]['create_time_text']!==$v['create_time_text']){
                $visit_list[$date][] = $v;
              }
            }else{
              $visit_list[$date][] = $v;
            }
        }
        return $visit_list;
    }

    /**
    * 采购日期页面
    * @param int $caigou_id 采购id  
    **/
    public function caigou_detail(){
      $one_data = Db::name('caigou_detail')->where('id',input('caigou_id'))->find();
      $date = $one_data['createtime'];
      $date = date('Y-m-d',$one_data['createtime']);
      // halt($date);
      $sql = "select *,FROM_UNIXTIME(createtime,'%Y-%m-%d')as date from `fa_caigou_detail` where date_format(from_unixtime(createtime),'%Y-%m-%d') = ('$date')";
      // halt($sql);
// ,FROM_UNIXTIME(createtime,'%Y-%m-%d')as date
      $list = Db::query($sql);

      $sum = Db::query("select sum(total_price) as money_sum,sum(num) as num_sum from `fa_caigou_detail` where date_format(from_unixtime(createtime),'%Y-%m-%d') = ('$date')");

      $return['list'] = $list;
      $return['number_data'] = $sum[0];
      $return['number_data']['date'] = $date;

      $this->success('采购日期页面',$return);

    }






}
